package cn.jr.action;

import java.util.Date;
import java.util.UUID;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;

import cn.jr.model.User;
import cn.jr.service.UserServiceI;

import com.opensymphony.xwork2.ModelDriven;

@Action(value = "user")
public class UserAction extends BaseAction implements ModelDriven<User> {

	private static final long serialVersionUID = 1L;

	User user = new User();

	@Override
	public User getModel() {
		return user;
	}

	@Resource
	private UserServiceI userService;

	
	public String login() {
		return "login";
	}
	
	public String toLogin() {
		user.setUserId(UUID.randomUUID().toString());
		user.setUserName("tom");
		user.setPassWord("tom");
		user.setRecordTime(System.currentTimeMillis() + "");
		userService.add(user);
		return "main";
	}
	
	public String add() {
		try {
			user.setUserId(UUID.randomUUID().toString());
			user.setUserName("tom");
			user.setPassWord("tom");
			user.setRecordTime(System.currentTimeMillis() + "");
			userService.add(user);
			
			super.writeJson(true);

		} catch (Exception e) {
			e.printStackTrace();
			super.writeJson(false);
		}
		return "add";
	}
	
	public String index() {
		return "index";
	}
}
